package com.qf.controller;


import com.qf.entity.R;
import com.qf.entity.User;
import com.qf.service.UserService;
import com.qf.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2020-11-30 09:27:52
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;

    /**
     * 注册的接口
     * @param user
     * @return
     */
    @RequestMapping("/register")
    public R register(User user){
        //进行注册
        boolean flag = userService.save(user);
        return new R().setData(flag);
    }

    /**
     * 用户登录 - jwt 保存用户登录状态到客户端
     * @return
     */
    @RequestMapping("/login")
    public R<String> login(String username, String password, String devid, HttpServletResponse response){

        User user = userService.login(username, password);
        if (user != null){
            //登录成功

            //通过JWT生成token
            String token = JwtUtil.getToken(user, devid);
            return new R().setData(token);
        }
        return new R().setCode(R.CODE.ERROR).setMsg("用户名或者密码错误！");
    }
}